package gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

import client.Cart;
import client.Main;
import client.MemberAccount;
import client.MainScreen;
import client.MyTableModel;
import client.Order;
import client.RemoveItems;
import client.setImage;

public class Order_GUI extends JFrame implements ActionListener 
{
	public setImage img = null;
	public JButton logOut = null;
	public JButton Back = null;
	public JButton createnew = null;
	public JButton cancel = new JButton();
	public JButton details = new JButton();
	public static JTable table = null;
	public JScrollPane ScrollPane = null;
	public Cart userCart = null;
	public String[] columnNames = {"Order Id", "Date Created", "Shipping Date", "Price", "Order Details", "Cancel Order"};
	public Object[][] Data = null;
	public static String callFrom = null;
	public Search_GUI newOrder = null;
	int width = (int)Main.dim.getWidth();
	int height = (int)Main.dim.getHeight();
	Object[] orderObj=new Object[3];
	
	public  Order_GUI(String call)
	{
		callFrom = call;
		
		if(Main.main.orderCallFrom.equals("CreditCard_GUI") || Main.main.orderCallFrom.equals("Payment_GUI"))
		{
			if(Main.main.order_control != null)	
				Main.main.order_control = null;
			
			Main.main.order_control = new Order(Cart_Gui.total, Cart_Gui.itemsForOrder, Cart_Gui.customItemsForOrder, Main.cart.deliveryGreeting.getData());
			Main.main.order_control = (Order)Main.clie.getResult();
			
			Main.main.order_control = null;
			Main.main.order_control = new Order();
			Main.main.order_control = (Order)Main.clie.getResult();
			Data = Main.main.order_control.data;
		}
		else 
		{
			if(Main.main.order_control != null)	
				Main.main.order_control = null;
			
			Main.main.order_control = new Order();
			Main.main.order_control = (Order)Main.clie.getResult();
			Data = Main.main.order_control.data;
		}
			
		
		img = new setImage("order");
		img.setLayout(null);
		
		this.setTitle("Custom Item");
		this.setPreferredSize(new Dimension(width-(width/2)+100, height-(height/3)-190));
		this.setLocation((int)Main.dim.getWidth()/2 - (int)this.getPreferredSize().getWidth()/2,(int)Main.dim.getHeight()/2 - (int)this.getPreferredSize().getHeight()/2);
		this.setResizable(false);
		
		final DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();  
        dtcr.setHorizontalTextPosition(JLabel.CENTER);  
       
		table = new JTable(new MyTableModel(Data, columnNames, "order"));
	    table.setPreferredScrollableViewportSize(new Dimension(450, 200));
        table.setFillsViewportHeight(true);
        table.setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,18));
        table.setIntercellSpacing(new Dimension(20,20));
		table.getTableHeader().setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,20));
		table.getTableHeader().setForeground(new Color(92,64,51));
        table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        table.getColumnModel().getColumn(0).setCellRenderer(dtcr);
        table.getColumnModel().getColumn(2).setCellRenderer(dtcr);
        table.getColumnModel().getColumn(4).setCellRenderer(dtcr);
        table.setBackground(new Color(253,245,230));
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      
        table.getColumnModel().getColumn(0).setPreferredWidth(100);	//Order Id
        table.getColumnModel().getColumn(1).setPreferredWidth(210);	//Date Created
        table.getColumnModel().getColumn(2).setPreferredWidth(180);	//Shipping Date
        table.getColumnModel().getColumn(3).setPreferredWidth(85);	//Price
        table.getColumnModel().getColumn(4).setPreferredWidth(140);	//Order Details
        table.getColumnModel().getColumn(5).setPreferredWidth(140);	//Remove Order
        table.setBackground(new Color(253,245,230));
        table.setRowHeight(55);
        
        table.getColumnModel().getColumn(4).setCellRenderer(new ButtonRenderer());
        table.getColumnModel().getColumn(4).setCellEditor(new ButtonEditor(new JCheckBox()));
        details.addActionListener(this);
        table.getColumnModel().getColumn(5).setCellRenderer(new ButtonRenderer());
        table.getColumnModel().getColumn(5).setCellEditor(new ButtonEditor(new JCheckBox()));
        cancel.addActionListener(this);
        
        ScrollPane = new JScrollPane(table);
        ScrollPane.setBounds(10, 80, width-(width/2)+75, height-(height/3)-370);
		img.add(ScrollPane);
		
		logOut = new JButton("LogOut");
		logOut.setBounds(10,10,100,40);
		logOut.addActionListener(this);
		img.add(logOut);
		
		Back = new JButton("Back");
		Back.setBounds((int)this.getPreferredSize().getWidth()-115,10,100,40);
		Back.addActionListener(this);
		img.add(Back);
		
		createnew = new JButton("Create New Order");
		createnew.setFont(new Font (Font.SANS_SERIF ,Font.PLAIN,20));
		createnew.setBounds(200, height-(height/3)-280, (int)this.getPreferredSize().getWidth()-400, 50);
		createnew.addActionListener(this);
		img.add(createnew);
		
		this.addWindowListener(new WindowAdapter() 
        {public void windowClosing(WindowEvent e)
        {Back.doClick();}});
		
		this.add(img);
		this.pack();
		
		this.setEnabled(true);
		this.setVisible(true);	
			
	}

	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == Back)
		{			
			Main.main.callFrom="MainScreen";
			Main.main.showMainScreen();
			this.dispose();
		}
		
		if(e.getSource() == createnew)
		{			
			Main.main.callFrom = "order_GUI";
			Main.main.search.doClick();
			this.setEnabled(false);
		}
		
		if(e.getSource() == cancel)
		{

		    //return 0 if not cancel, 1 if more than 3 hours before ready 100%

			if(Main.main.order_control != null)	
				Main.main.order_control = null;
			Main.main.order_control=new Order();//return 0 if not cancel, 1 if more than 3 hours before ready 100%

			//returns 2 if between 1-3 hours before 50%, less than 1 hour 0%

			
			 	Main.main.order_control=new Order();
			 	int j=(Integer)table.getValueAt(table.getSelectedRow(), 0);	
		        orderObj= Main.main.order_control.cancelCheck(j);
		        int i=(Integer)orderObj[2];
		        Float totalprice=(Float)orderObj[1];
		        Float total50=(float) (totalprice*0.5);
		        String uid=orderObj[0].toString();
		        int choice=-1;
			    if(i==0)		JOptionPane.showMessageDialog(null,"You can't cancel - your order had been shipped\n if u didn't get it \n U can contacte service employee and file complaint!  ","Cancel Order - ERROR",JOptionPane.ERROR_MESSAGE);
			    if(i==1)
			    {
			    	choice=JOptionPane.showConfirmDialog(null," are u sure u want to cancel? \n if yes-Your balance will be compensated by: "+totalprice+" a 100% of your total order cost" ,"Order Cancel ",JOptionPane.OK_CANCEL_OPTION);
			    	if(choice==0)
			    	{
			    		Main.main.order_control.removeOrder(j);
			    		String money=totalprice.toString();
			    		MemberAccount.SetBalance(uid,money,"-1");
			    	}
			    }
			    if(i==2)
			    {
			    	choice=JOptionPane.showConfirmDialog(null," are u sure u want to cancel? \n if yes- Your balance will be compensated by: "+total50+" a 50% of your order cost  ","Order Cancel ",JOptionPane.OK_CANCEL_OPTION);
			    	if(choice==0)
			    	{
			    		total50=totalprice;
			    		Main.main.order_control.removeOrder(j);
			    		String money=total50.toString();
			    		MemberAccount.SetBalance(uid,money,"-1");
			    	}
			    }
			    if(i==3)
			    {
			    	choice=JOptionPane.showConfirmDialog(null," are u sure u want to cancel? \n if yes - Your balance won't be compensated! - it is too late! ","Order Cancel ",JOptionPane.OK_CANCEL_OPTION);
			    	if(choice==0)
				    {
			    		Main.main.order_control.removeOrder(j);
			    		MemberAccount.SetBalance(uid,"0","-1");
				    }
			    }
	
		}
		
		if(e.getSource() == logOut)
		{
			if(JOptionPane.showConfirmDialog(null,"Are you sure you want to logout?",null,JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
			{
				Main.main.logOut.doClick();
				this.dispose();
			}
		}
	}
	
	class ButtonRenderer extends JButton implements TableCellRenderer 
    {
    	public ButtonRenderer() 
    	{setOpaque(true);}
    	public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column)
    	{	
    		if(column == 4)	
    			setText("Details");
    		else //(column == 5)
    				setText("Cancel");
    		return this;
    	}
    }
    
    class ButtonEditor extends DefaultCellEditor 
    {
    	private String label;
    	public ButtonEditor(JCheckBox checkBox) 
    	{
    		super(checkBox);
    	}
    	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) 
    	{
    		if(column == 4)
    		{
        		label = "Details";
        		details.setText(label);
        		return details;
        	}
    		else //(column == 5)
    		{
        		label = "cancel";
        		cancel.setText(label);
        		return cancel;
        	}
    	}
    	public Object getCellEditorValue() 
    	{
    		return new String(label);
    	}
    }

}
